// pages/publish/publish.js
const API = require('../../common/constant.js');
const util = require('../../utils/util');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    eventInfo: {},
    typePick: {
      array: ['#求邂逅#', '#美食搭伙#', '#解闷聊天#'],
      index: '',
      isTypePick: false,
    },
    numPick: {
      array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      index: '',
      isNumPick: false
    },
    sexPick: {
      array: ['不限', '男', '女'],
      index: '',
      isSexPick: false
    },
    payPick: {
      array: ['我请客', 'AA制', '男A女免'],
      index: '',
      isPayPick: false
    },
    travelPick: {
      array: ['各自出行', '我接送（发起人接送）'],
      index: '',
      isTravelPick: false
    },
    eventAddress: "请选择"
  },
  typePickerChange: function (e) {
    this.setData({
      ['typePick.index']: e.detail.value,
      ['typePick.isTypePick']: true
    })
  },
  numPickerChange: function (e) {
    this.setData({
      ['numPick.index']: e.detail.value,
      ['numPick.isNumPick']: true
    })
  },
  sexPickerChange: function (e) {
    this.setData({
      ['sexPick.index']: e.detail.value,
      ['sexPick.isSexPick']: true
    })
  },
  payPickerChange: function (e) {
    this.setData({
      ['payPick.index']: e.detail.value,
      ['payPick.isPayPick']: true
    })
  },
  travelPickerChange: function (e) {
    this.setData({
      ['travelPick.index']: e.detail.value,
      ['travelPick.isTravelPick']: true
    })
  },
  bindDateChange: function (e) {
    if (e.target.id === 'registrationData') {
      this.setData({
        registrationData: e.detail.value,
        isRegistrationData: true
      })
    } else {
      this.setData({
        eventData: e.detail.value,
        isEventData: true
      })
    }
  },
  bindTimeChange: function (e) {
    if (e.target.id === 'registrationStartTime') {
      this.setData({
        registrationStartTime: e.detail.value,
        isRegistrationStartTime: true
      })
    } else if (e.target.id === 'registrationEndTime') {
      this.setData({
        registrationEndTime: e.detail.value,
        isRegistrationEndTime: true,
      })
    } else if (e.target.id === 'eventStartTime') {
      this.setData({
        eventStartTime: e.detail.value,
        isEventStartTime: true,
      })
    } else {
      this.setData({
        eventEndTime: e.detail.value,
        isEventEndTime: true,
      })
    }
  },
  getTitle: function (e) {
    this.setData({
      eventTitle: e.detail.value
    })
  },
  getEventContent: function (e) {
    this.setData({
      eventContent: e.detail.value
    })
  },

  // 得到发布活动的信息
  getPublishInfo: function () {
    let eventInfo = this.data.eventInfo;
    let eventTitle = this.data.eventTitle;
    let eventContent = this.data.eventContent;
    if(eventTitle&&eventTitle.trim() != ""){
      eventInfo.eventTitle = eventTitle;
    }
    if (eventContent && eventContent.trim() != "") {
      eventInfo.eventContent = eventContent;
    }
    if (this.data.eventAddress != '请选择') {
      eventInfo.eventAddress = this.data.eventAddress;
    }
    if (this.data.typePick.index === "0") {
      eventInfo.eventType = "find"
    } else if (this.data.typePick.index === "1") {
      eventInfo.eventType = 'partner'
    } else if (this.data.typePick.index === "2") {
      eventInfo.eventType = 'chat'
    }
    eventInfo.registrationSex = this.data.sexPick.index + "";
    eventInfo.registrationStartTime = this.data.registrationData + " " + this.data.registrationStartTime + ':00';
    eventInfo.registrationEndTime = this.data.registrationData + " " + this.data.registrationEndTime + ':00';
    eventInfo.eventStartTime = this.data.eventData + " " + this.data.eventStartTime + ':00';
    eventInfo.eventEndTime = this.data.eventData + " " + this.data.eventEndTime + ':00';
    if (this.data.payPick.index === "0") {
      eventInfo.payMethod = "me_treat"
    } else if (this.data.payPick.index === "1") {
      eventInfo.payMethod = 'aa'
    } else if (this.data.payPick.index === "2") {
      eventInfo.payMethod = 'male_a_female_free'
    }
    eventInfo.eventNumber = this.data.numPick.array[this.data.numPick.index];
    if (this.data.travelPick.index === "0") {
      eventInfo.travelModel = 'self'
    } else if (this.data.travelPick.index === "1") {
      eventInfo.travelModel = 'me_transfer'
    }
    eventInfo.longitude = this.data.longitude;
    eventInfo.latitude = this.data.latitude;
    eventInfo.cityCode = this.data.cityCode;
    let state = false;
    this.setData({
      eventInfo: eventInfo
    })
    if (this.data.eventData && this.data.eventStartTime && this.data.eventEndTime && this.data.registrationData && this.data.registrationStartTime && this.data.registrationEndTime) {
      state = true
    }
    if (!this.data.eventData && !this.data.eventStartTime && !this.data.eventEndTime && !this.data.registrationData && !this.data.registrationStartTime && !this.data.registrationEndTime){
      state = false
    }
    if (!state) {
      wx.showToast({
        title: '请填写正确时间',
        icon: "none",
        duration: 2000
      })
      return
    }
    let that = this;
    wx.showModal({
      title: '修改饭约',
      content: '是否修改',
      success(res) {
        console.log(res)
        if (res.confirm) {
          that.saveEventInfo(eventInfo);
        }
      }
    })
  },

  // 发布活动
  saveEventInfo: function (params) {
    params.id = this.data.id;
    util.http({
      url: API.URL.EVENT.EDIT_EVENTINFO,
      data: {
        ...params
      },
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: '修改成功',
          duration: 1500
        })
        setTimeout(function () {
          wx.navigateBack();
        }, 1500)
      },
      fail: function (res) {
        console.log(res);
        app.showUserInfo(res.msg, 'none', 2000);
      }
    })
  },

  onLoad: function (options) {
    this.setData({
      id: options.id
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    let globalData = app.globalData;
    if (globalData.title) {
      this.setData({
        eventAddress: globalData.title,
        longitude: globalData.longitude,
        latitude: globalData.latitude,
        cityCode: globalData.cityCode
      })
      app.globalData.title = '';
      app.globalData.longitude = '';
      app.globalData.latitude = '';
      app.globalData.cityCode = '';
    }
    else {
      this.setData({
        eventAddress: "请选择"
      })
    }
  }
})